<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/8/12
 * Time: 15:33
 */

namespace app\controller\index;
use app\model\index\Web AS model;

class Web extends Base
{
    public function list(){
        $where = [];
//        $where[] = ['user_id','=',$this->user->id];
        $get = input('get.');
        !empty($get['web']) && $where[] = ['web','like','%'.$get['web'].'%'];
        !empty($get['name']) && $where[] = ['name','like','%'.$get['name'].'%'];
        $list = model::where($where)
            ->with(['user_data'])
            ->order('id DESC')
            ->select();
        return $this->view('/index/web/list',['list' => $list,'get' => $get]);
    }

    public function info($id = 0){
        $info = model::find($id);
        return $this->msg($info);
    }

    public function save($id = 0){
        $post = input('post.');
        if (empty($id)) {
            $count = (new model())->where('web', $post['web'])->count();
            if ($count > 0)
                return $this->fail('网站已经存在');
            $post['user_id'] = $this->user->id;
            $post['create_time'] = date('Y-m-d');
            $website = cache('website');
            $website[] = [
                'website' => $post['web'],
                'name' => $post['name']
            ];
            cache('website', $website);
            model::create($post);
        }else{
            $find = model::find($id);
            $find->save($post);
        }
        return $this->msg();
    }

    public function del($id){
        $web = model::where('id',$id)->value('web');
        $website = cache('website');
        foreach ($website AS $key => $value){
            if ($value['website'] == $web)
                unset($website[$key]);
        }
        foreach ($website AS $key => $value)
            $array[] = $value;
        if (!empty($array))
            cache('website',$array);
        model::where('id',$id)->delete();
        return $this->msg();
    }

    public function emailtokehu(){
        $title = input('title');
        $body = input('body');
        $email = '';
        send_email($email,$title,$body);
        return $this->msg();
    }
}